<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8" style="margin-top:20px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['distributor:distributorList:edit']"
          >添加分销商</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="postList">
      <el-table-column width="70" type="index" label="序号" align="center" />
      <el-table-column label="分销商名称" align="center" prop="tradername" />
      <el-table-column label="分销商账号" align="center" prop="traderaccount" />
      <el-table-column label="秘钥" align="center" prop="traderkey" />
      <el-table-column label="日可访问量" align="center" prop="dailyAmount" />
      <el-table-column label="可售票数" align="center" prop="tkAmount" />
      <el-table-column label="可超售票数" align="center" prop="tkOverAmount" />
      <el-table-column label="账户余额" align="center" prop="accountBalance" />
      <el-table-column
        label="可透余额"
        align="center"
        prop="overAccountBalance"
      />
      <el-table-column
        label="服务费配置"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="getParticulars(scope.row)"
            v-hasPermi="['distributor:distributorList:edit']"
            >查看</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        label="是否启用"
        align="center"
        prop="sellstatus"
        width="180"
      >
        <template slot-scope="scope">
          <p v-if="postList[scope.$index].stopflag == 1">是</p>
          <p v-if="postList[scope.$index].stopflag == 0">否</p>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['distributor:distributorList:edit']"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="recharges(scope.row)"
            v-hasPermi="['distributor:distributorList:edit']"
            >充值</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageIndex"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加分销商 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-tabs v-model="activeName" type="border-card">
        <el-tab-pane label="分销商信息" name="first">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="分销商名称:"
                  prop="tradername"
                  label-width="115px"
                >
                  <el-input v-model="form.tradername" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="分销商账号:"
                  prop="traderaccount"
                  label-width="115px"
                >
                  <el-input v-model="form.traderaccount" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="秘钥:"
                  prop="traderkey"
                  label-width="115px"
                >
                  <el-input v-model="form.traderkey" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="日可访问量:"
                  prop="dailyAmount"
                  label-width="115px"
                >
                  <el-input v-model="form.dailyAmount" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="可售票数:"
                  prop="tkAmount"
                  label-width="115px"
                >
                  <el-input v-model="form.tkAmount" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="可超票数:"
                  prop="tkOverAmount"
                  label-width="115px"
                >
                  <el-input v-model="form.tkOverAmount" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="账户余额:"
                  prop="accountBalance"
                  label-width="115px"
                >
                  <el-input
                    v-model="form.accountBalance"
                    placeholder="请输入"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="可透余额:"
                  prop="overAccountBalance"
                  label-width="115px"
                >
                  <el-input
                    v-model="form.overAccountBalance"
                    placeholder="请输入"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="是否启用:"
                  prop="stopflag"
                  label-width="115px"
                >
                  <el-switch
                    v-model="form.stopflag"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    active-value="1"
                    inactive-value="0"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
        <!-- 服务费配置 -->
        <el-tab-pane label="服务费配置" name="second">
          <el-form :model="form" :rules="rules" label-width="80px">
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="收费类型:"
                  prop="feetype"
                  label-width="115px"
                >
                  <el-radio v-model="form.feetype" label="1">按车票</el-radio>
                  <el-radio v-model="form.feetype" label="2">按订单</el-radio>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="收费模式:"
                  prop="feemodel"
                  label-width="115px"
                >
                  <el-radio v-model="form.feemodel" label="1"
                    >定额收费</el-radio
                  >
                  <el-radio v-model="form.feemodel" label="2"
                    >阶梯收费</el-radio
                  >
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col>
                <el-form-item
                  label="阶梯服务费配置:"
                  prop="qph"
                  label-width="115px"
                >
                  <div style="display:flex;margin-top:10px;">
                    <el-input
                      v-model="sectionfeeList[0].start"
                      style="width:80px"
                    />~
                    <el-input
                      v-model="sectionfeeList[0].end"
                      style="width:80px"
                    />，服务费:
                    <el-input
                      v-model="sectionfeeList[0].fee"
                      style="width:80px"
                    />
                  </div>
                  <div style="display:flex;margin-top:10px;">
                    <el-input
                      v-model="sectionfeeList[1].start"
                      style="width:80px"
                    />~
                    <el-input
                      v-model="sectionfeeList[1].end"
                      style="width:80px"
                    />，服务费:
                    <el-input
                      v-model="sectionfeeList[1].fee"
                      style="width:80px"
                    />
                  </div>
                  <div style="display:flex;margin-top:10px;">
                    <el-input
                      v-model="sectionfeeList[2].start"
                      style="width:80px"
                    />~
                    <el-input
                      v-model="sectionfeeList[2].end"
                      style="width:80px"
                    />，服务费:
                    <el-input
                      v-model="sectionfeeList[2].fee"
                      style="width:80px"
                    />
                  </div>
                  <div style="display:flex;margin-top:10px;">
                    <el-input
                      v-model="sectionfeeList[3].start"
                      style="width:80px"
                    />~
                    <el-input
                      v-model="sectionfeeList[3].end"
                      style="width:80px"
                    />，服务费:
                    <el-input
                      v-model="sectionfeeList[3].fee"
                      style="width:80px"
                    />
                  </div>
                  <div style="display:flex;margin-top:10px;">
                    <el-input
                      v-model="sectionfeeList[4].start"
                      style="width:80px"
                    />~
                    <el-input
                      v-model="sectionfeeList[4].end"
                      style="width:80px"
                    />，服务费:
                    <el-input
                      v-model="sectionfeeList[4].fee"
                      style="width:80px"
                    />
                  </div>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查看服务费配置 -->
    <el-dialog
      :title="title"
      :visible.sync="particulars"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="trader" label-width="80px">
        <el-row>
          <el-col>
            <el-form-item label="收费类型:" prop="feetype" label-width="115px">
              <p style="display:inline" v-if="trader.feetype == 1">按车票</p>
              <p style="display:inline" v-if="trader.feetype == 2">按订单</p>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="收费模式:" prop="feemodel" label-width="115px">
              <p style="display:inline" v-if="trader.feetype == 1">
                定额收费
              </p>
              <p style="display:inline" v-if="trader.feetype == 2">
                阶梯收费
              </p>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="阶梯收费配置:" prop="id" label-width="115px">
              <div
                v-for="(item, index) in trader.sectionfee"
                :key="index"
                style="margin-top:-12px"
              >
                <p style="height:30px" v-if="item.start != null">
                  价格￥{{ item.start }}~￥{{ item.end }},收取服务费{{
                    item.fee
                  }}
                </p>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="particulars = false">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 编辑分销商 -->
    <el-dialog
      :title="title"
      :visible.sync="compile"
      width="600px"
      append-to-body
    >
      <el-tabs v-model="activeName" type="border-card">
        <el-tab-pane label="分销商信息" name="first">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="分销商名称:"
                  prop="tradername"
                  label-width="115px"
                >
                  <el-input v-model="form.tradername" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="分销商账号:"
                  prop="traderaccount"
                  label-width="115px"
                >
                  <el-input v-model="form.traderaccount" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="秘钥:"
                  prop="traderkey"
                  label-width="115px"
                >
                  <el-input v-model="form.traderkey" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="日可访问量:"
                  prop="dailyAmount"
                  label-width="115px"
                >
                  <el-input v-model="form.dailyAmount" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="可超票数:"
                  prop="tkOverAmount"
                  label-width="115px"
                >
                  <el-input v-model="form.tkOverAmount" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="可透余额:"
                  prop="overAccountBalance"
                  label-width="115px"
                >
                  <el-input
                    v-model="form.overAccountBalance"
                    placeholder="请输入"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="是否启用:"
                  prop="stopflag"
                  label-width="115px"
                >
                  <el-switch
                    v-model="form.stopflag"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    active-value="1"
                    inactive-value="0"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
        <!-- 服务费配置 -->
        <el-tab-pane label="服务费配置" name="second">
          <el-form :model="form" :rules="rules" label-width="80px">
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="收费类型:"
                  prop="feetype"
                  label-width="115px"
                >
                  <el-radio v-model="form.feetype" label="1">按车票</el-radio>
                  <el-radio v-model="form.feetype" label="2">按订单</el-radio>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item
                  label="收费模式:"
                  prop="feemodel"
                  label-width="115px"
                >
                  <el-radio v-model="form.feemodel" label="1"
                    >定额收费</el-radio
                  >
                  <el-radio v-model="form.feemodel" label="2"
                    >阶梯收费</el-radio
                  >
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col>
                <el-form-item
                  label="阶梯服务费配置:"
                  prop="qph"
                  label-width="115px"
                >
                  <div style="display:flex;margin-top:10px;">
                    <el-input
                      v-model="sectionfeeList[0].start"
                      style="width:80px"
                    />~
                    <el-input
                      v-model="sectionfeeList[0].end"
                      style="width:80px"
                    />，服务费:
                    <el-input
                      v-model="sectionfeeList[0].fee"
                      style="width:80px"
                    />
                  </div>
                  <div style="display:flex;margin-top:10px;">
                    <el-input
                      v-model="sectionfeeList[1].start"
                      style="width:80px"
                    />~
                    <el-input
                      v-model="sectionfeeList[1].end"
                      style="width:80px"
                    />，服务费:
                    <el-input
                      v-model="sectionfeeList[1].fee"
                      style="width:80px"
                    />
                  </div>
                  <div style="display:flex;margin-top:10px;">
                    <el-input
                      v-model="sectionfeeList[2].start"
                      style="width:80px"
                    />~
                    <el-input
                      v-model="sectionfeeList[2].end"
                      style="width:80px"
                    />，服务费:
                    <el-input
                      v-model="sectionfeeList[2].fee"
                      style="width:80px"
                    />
                  </div>
                  <div style="display:flex;margin-top:10px;">
                    <el-input
                      v-model="sectionfeeList[3].start"
                      style="width:80px"
                    />~
                    <el-input
                      v-model="sectionfeeList[3].end"
                      style="width:80px"
                    />，服务费:
                    <el-input
                      v-model="sectionfeeList[3].fee"
                      style="width:80px"
                    />
                  </div>
                  <div style="display:flex;margin-top:10px;">
                    <el-input
                      v-model="sectionfeeList[4].start"
                      style="width:80px"
                    />~
                    <el-input
                      v-model="sectionfeeList[4].end"
                      style="width:80px"
                    />，服务费:
                    <el-input
                      v-model="sectionfeeList[4].fee"
                      style="width:80px"
                    />
                  </div>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCompile">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 分销商充值 -->
    <el-dialog
      :title="title"
      :visible.sync="configuration"
      width="500px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="rechargeList"
        :rules="rules"
        label-width="80px"
      >
        <el-row>
          <el-col :span="18">
            <el-form-item
              label="分销商:"
              prop="machinecode"
              label-width="115px"
            >
              <el-input
                v-model="rechargeList.tradername"
                placeholder="请输入"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="增加可售票数:" prop="dqph" label-width="115px">
              <el-input v-model="rechargeList.tkamount" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="账户充值金额:" prop="qph" label-width="115px">
              <el-input
                v-model="rechargeList.accountbalance"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="getSubmitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPage,
  traderFee,
  add,
  recharge,
  update,
  modify
} from "@/api/distributor/distributorList";
export default {
  name: "DistributorList",
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first", //添加切换
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 添加分销商弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      //查看服务费配置弹出框
      particulars: false,
      //分销商充值弹出框
      configuration: false,
      //编辑弹出框
      compile: false,
      // 服务费配置弹出窗
      serviceallocation: false,
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10
      },
      //查看服务费配置参数
      trader: {},
      sectionfeeList: [
        {
          start: null,
          end: null,
          fee: null
        },
        {
          start: null,
          end: null,
          fee: null
        },
        {
          start: null,
          end: null,
          fee: null
        },
        {
          start: null,
          end: null,
          fee: null
        },
        {
          start: null,
          end: null,
          fee: null
        }
      ],
      // 充值参数
      rechargeList: {
        tradername: "",
        traderid: "",
        tkamount: "",
        accountbalance: ""
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tradername: [
          { required: true, message: "分销商名称不能为空", trigger: "blur" }
        ],
        traderaccount: [
          { required: true, message: "分销商账号不能为空", trigger: "blur" }
        ],
        traderkey: [
          { required: true, message: "秘钥不能为空", trigger: "blur" }
        ],
        dailyAmount: [
          { required: true, message: "日访问量不能为空", trigger: "blur" }
        ],
        tkAmount: [
          { required: true, message: "可售票数不能为空", trigger: "blur" }
        ],
        tkOverAmount: [
          { required: true, message: "可超票数不能为空", trigger: "blur" }
        ],
        accountBalance: [
          { required: true, message: "账户余额不能为空", trigger: "blur" }
        ],
        overAccountBalance: [
          { required: true, message: "可透余额不能为空", trigger: "blur" }
        ],
        feetype: [
          { required: true, message: "请选择收费类型", trigger: "blur" }
        ],
        feemodel: [
          { required: true, message: "请选择收费模式", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList(); //分销商列表
  },
  methods: {
    /** 查询分销商列表 */
    getList() {
      this.loading = true;
      getPage(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.particulars = false;
      this.configuration = false;
      this.compile = false;
      this.serviceallocation = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        traderid: undefined,
        tradername: undefined,
        traderaccount: undefined,
        traderkey: undefined,
        dailyAmount: undefined,
        tkAmount: undefined,
        tkOverAmount: undefined,
        accountBalance: undefined,
        overAccountBalance: undefined,
        stopflag: "0",
        trader: undefined,
        feetype: undefined,
        feemodel: undefined,
        fixfee: undefined,
        sectionfee: undefined,
        isenable: "1",
        bak1: undefined
      };
    },
    /** 添加分销商 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加分销商";
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    //查看服务费配置
    getParticulars(row) {
      this.title = "服务费配置";
      traderFee({ traderId: row.traderaccount }).then(response => {
        if (response.code == 200) {
          this.particulars = true;
          this.trader = response.data;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    //充值按钮
    recharges(row) {
      this.title = "充值";
      this.configuration = true;
      this.rechargeList = {};
      this.rechargeList.tradername = row.tradername;
      this.rechargeList.traderid = row.traderid;
    },
    // 充值
    getSubmitForm() {
      recharge(this.rechargeList).then(response => {
        if (response.code == 200) {
          this.configuration = false;
          this.$message({
            message: "充值成功",
            type: "success"
          });
          this.getList();
        } else {
          this.$message.error(response.msg);
        }
      });
    },
    /** 编辑按钮操作 */
    handleUpdate(row) {
      traderFee({ traderId: row.traderaccount }).then(response => {
        if (response.code == 200) {
          this.form.feetype = response.data.feetype;
          this.form.feemodel = response.data.feemodel;
          this.form.sectionfee = response.data.sectionfee;
        } else {
          this.$message.error(res.msg);
        }
      });
      this.reset();
      this.form = row;
      this.compile = true;
      this.title = "修改分销商";
      this.form.id = row.traderid;
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    // 编辑确定按钮
    submitCompile() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.trader = this.form.traderaccount;
          this.form.sectionfee = JSON.stringify(this.sectionfeeList);
          if (
            this.form.feetype != null &&
            this.form.feemodel != null &&
            this.form.sectionfee != null
          ) {
            update(this.form).then(response => {
              if (response.code == 200) {
                this.compile = false;
                this.getList();
              } else {
                this.$message.error(response.msg);
              }
            });
            modify(this.form).then(response => {
              this.serviceallocation = false;
              this.getList();
            });
          } else {
            this.$message({
              showClose: true,
              message: "请输入服务费配置",
              type: "warning"
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.trader = this.form.traderaccount;
          this.form.sectionfee = JSON.stringify(this.sectionfeeList);
          if (
            this.form.feetype != null &&
            this.form.feemodel != null &&
            this.form.sectionfee != null
          ) {
            add(this.form).then(response => {
              if (response.code == 200) {
                this.open = false;
                this.getList();
              } else {
                this.$message.error(response.msg);
              }
            });
            modify(this.form).then(response => {
              this.serviceallocation = false;
              this.getList();
            });
          } else {
            this.$message({
              showClose: true,
              message: "请输入服务费配置",
              type: "warning"
            });
          }
        }
      });
    }
  }
};
</script>
